<template>
  <view class="trainingcenter-content">
    <!-- 培训中心 -->
    <view v-for="(item, index) in menuItems" :key="index" class="menu-item" @click="handleTap(item)">
      <image :src="item.icon" class="menu-icon" mode="aspectFit" />
      <text class="menu-text">{{ item.title }}</text>
    </view>
  </view>
</template>

<script setup>
import { shopApi } from "@/api";
import { IMG_URL2 } from "@/config";
const menuItems = ref([
  {
    title: "扫码入库",
    icon: IMG_URL2 + "inventory/scan.svg",
    path: "/pagesshop/inventory/scanpage",
    type: "1"
  },
  {
    title: "选择采购订单入库",
    icon: IMG_URL2 + "inventory/root-list.svg",
    path: "/pagesshop/inventory/chooseorder",
    type: "2"
  },
  {
    title: "手动输入款号",
    icon: IMG_URL2 + "inventory/root-list.svg",
    path: "/pagesshop/inventory/addproductnum",
    type: "4"
  },
  {
    title: "手动添加其他品牌商品",
    icon: IMG_URL2 + "inventory/edit.svg",
    path: "/pagesshop/inventory/addproduct",
    type: "3"
  }
]);

const handleTap = (item) => {
  if (item.type === "1") {
    uni.scanCode({
      success: (res1) => {
        console.log(res1, "res1");
        // 商品入库 - 根据商品规格编码
        shopApi.GETFRONTPRODUCTINSTOCKBYSKUCODE({
          productSkuCode: res1.result
        }).then((res) => {
          if (res.code != 200) {
            uni.showToast({
              title: res.errMsg,
              icon: "none"
            });
            return;
          }
          uni.navigateTo({
            url: `${item.path}?code=${res1.result}`
          });
        });
      }
    });
    return;
  }
  uni.navigateTo({
    url: item.path,
    fail(err) {
      console.error("Navigation failed:", err);
      uni.showToast({
        title: "页面跳转失败",
        icon: "none"
      });
    }
  });
};
// 
</script>
<style lang="scss">
page {
  background-color: #f3f3f3;
}
</style>
<style lang="scss" scoped>
.trainingcenter-content {
  padding: 20rpx;
  box-sizing: border-box;
  .menu-item {
    display: flex;
    align-items: center;
    background-color: #ffffff;
    padding: 30rpx;
    box-sizing: border-box;
    margin-bottom: 20rpx;
    border-radius: 12rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  }

  .menu-icon {
    width: 40rpx;
    height: 40rpx;
    margin-right: 20rpx;
  }

  .menu-text {
    font-weight: 400;
    font-size: 28rpx;
    color: rgba(44, 44, 44, 1);
  }
}
</style>
